//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#if !defined (__KEYPAD_H__)
#define __KEYPAD_H__

#include <ddk.h>
#include "nu_api.h"
#include "drvassistant.h"

#define SYNCHRONIZATION     0
#define ASYNCHRONISM        1

#define TOTALKEYNUMBER      8 + 2 + 1//2 repeat timer 1 for LINECONTROL

#define BUFFERSIZE 128

typedef struct KPD_DATA {
    unsigned long keycode;
    unsigned long event_id; //key press or release status
} KPD_DATA;

// keypad status
#define KEYDOWN ((unsigned long)(1UL << 31))
#define KEYUP   (0)

class CKeypad : public CDeviceDriver {
public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 offset,
        /* [in] */ const MemoryBuf &buffer,
        /* [out] */ Int32* pBytesWritten,
        /* [out] */ IEvent** ppCompletionEvent) {
        return E_NOT_IMPLEMENTED;
    }

    CARAPI Control(
        /* [in] */ Handle32 controlCode,
        /* [in] */ const MemoryBuf &inBuffer,
        /* [out] */ MemoryBuf* pOutBuffer,
        /* [out] */ IEvent** ppCompletionEvent);

    virtual void Dispose() {}

    CKeypad();
    ECode IsPushUp(unsigned long keycode, Boolean *pbHavePush);
    int SetPushMap(unsigned long keycode, int *pPushMap);
    ECode IsRepeatKeyCode(unsigned long keycode);

//    int SetRepeatCallback();
//    void KeypadPressCallBack(unsigned char act_key);
//    void KeypadReleaseCallBack(unsigned char act_key);
//    void KeypadPushCallback(unsigned long keycode, int *pbPushMap);

public :
    DzEvent m_event;
    Int32 m_pushUp;

    volatile Int32 m_bRepeat;
    volatile Int32 m_nRepeatIndex;

    volatile Int32 m_keyIndex;
    volatile Int32 m_keyRIndex;
    Int32 m_bRead;

    KPD_DATA m_pNuKeyData[BUFFERSIZE];
    KPD_DATA m_buffer[BUFFERSIZE];

    Int32 m_head;
    Int32 m_tail;
    // design for key long press
    DzTimer *m_pTimers[TOTALKEYNUMBER];
};


#endif
